package com.longrise.android.byjk.plugins.dealsituation.course.video2.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.CourseTrainDirBean;
import com.longrise.android.byjk.plugins.dealsituation.course.cache.CourseCache;
import com.longrise.android.byjk.plugins.course.coursedetail.courseadapter.CourseDetailAdapter;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoParams;
import com.longrise.common.utils.PrintLog;
import com.longrise.common.utils.AppBaseDialog;

/**
 * Created by godliness on 2017/8/16.
 * 目录  V1.3.7
 */

public final class CatalogDialog extends AppBaseDialog implements CourseDetailAdapter.OnChildItemClickListener {

    private static final String TAG = "CatalogDialog";

    private CourseDetailAdapter mCourseDetailAdapter;
    private ExpandableListView mListView;

    private CourseDetailAdapter.OnChildItemClickListener mOnItemClick;

    private View mLlTitle;

    private String mCourseId = "";
    private String mCwid = "";

    private int[] mPosition;

    /**
     * 是否是下载详情进入
     */
    private boolean mFromCache;

    public CatalogDialog(Context context) {
        super(context, R.style.VideoListDialog);

        initFlag();
    }

    private void initFlag() {
        if (CourseCache.Flag.FLAG_FROM_COURSE_DOWNLOADED.equals(CourseCache.getCacheFlag())) {
            mFromCache = true;
        } else {
            mFromCache = false;
        }
    }

    /**
     * @param courseId 课程id
     * @param cwid     课件id
     */
    public void setParams(String courseId, String cwid) {
        this.mCourseId = courseId;
        this.mCwid = cwid;
    }

    /**
     * @param courseid 根据课程id查询本地课件
     *                 查询已缓存课程结构，要区分已缓存课程详情页与课程详情页两种情况
     */
    private String cacheFromLocal(String courseid) {
        if (mFromCache) {
            return CourseCache.cacheFromLocal(courseid, CourseCache.Flag.FLAG_FROM_COURSE_DOWNLOADED);
        }
        return CourseCache.cacheFromLocal(courseid, CourseCache.Flag.FLAG_FROM_COURSE_DETAIL);
    }

    @Override
    public int getLayoutResourceId(Bundle bundle) {
        rightInRightOut();
        return R.layout.dialog_catalog;
    }

    private void rightInRightOut() {
        Window window = getWindow();
        window.setGravity(Gravity.RIGHT);
        window.setWindowAnimations(R.style.dialog_in_out);
    }

    @Override
    public void init() {
        mLlTitle = findViewById(R.id.ll_cache_state);
        mLlTitle.setVisibility(mFromCache ? View.VISIBLE : View.GONE);
        mListView = (ExpandableListView) findViewById(R.id.list_catalog);
        setAdapter();

        post(new Runnable() {
            @Override
            public void run() {
                loadCoruseCache();
            }
        });
    }

    private void setAdapter() {
        //static
        if (mCourseDetailAdapter == null) {
            mCourseDetailAdapter = new CourseDetailAdapter();
        }
        mCourseDetailAdapter.setItemSelector(R.drawable.selecter_videolist_item);
        mListView.setAdapter(mCourseDetailAdapter);
        mListView.setGroupIndicator(null);
    }

    private void onItem(VideoParams videoParams) {
        if (videoParams == null) {
            return;
        }
        if (!checkNetworkEnable()) {
            return;
        }
        if (TextUtils.isEmpty(videoParams.mCwidStyle)) {
            showToast("课件类型异常");
            return;
        }
        if (mOnItemClick != null) {
            mPosition = videoParams.mCwidLocal;
            mOnItemClick.onItemClick(videoParams);
        }
    }

    private void loadCoruseCache() {
        final String value = cacheFromLocal(mCourseId);
        if (TextUtils.isEmpty(value)) {
            return;
        }
        try {
            parseCourse(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void parseCourse(String value) throws Exception {
        Gson gson = new Gson();
        CourseTrainDirBean courseTrainDirBean = gson.fromJson(value, CourseTrainDirBean.class);
        updateDataInAdapter(courseTrainDirBean);
    }

    private void updateDataInAdapter(final CourseTrainDirBean bean) {
        if (mCourseDetailAdapter != null) {
            mCourseDetailAdapter.setData(bean, false, true);
        }
        locationInAdapter();
    }

    /**
     * 定位到cwid
     */
    private void locationInAdapter() {
        try {
            if (mPosition == null) {
                mPosition = mCourseDetailAdapter.getCwidInAdapterIndex(mCwid);
            }
            if (mPosition != null) {
                mListView.expandGroup(mPosition[0]);
                mListView.setSelectedChild(mPosition[0], mPosition[1], true);
            }
            PrintLog.e(TAG, "position: " + mPosition[0] + " " + mPosition[1]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void post(Runnable runnable) {
        if (mListView != null) {
            mListView.post(runnable);
        }
    }

    @Override
    public void onItemClick(VideoParams params) {
        dismiss();
        if (mCwid.equals(params.mCwid)) {
            return;
        }
        onItem(params);
    }

    public void setOnItemClick(CourseDetailAdapter.OnChildItemClickListener onItemClick) {
        this.mOnItemClick = onItemClick;
    }

    @Override
    public void regEvent() {
        if (mCourseDetailAdapter != null) {
            mCourseDetailAdapter.setItemClickListener(this);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mCourseDetailAdapter = null;
    }
}
